// shopping-cart
const componentBehaviors = require('../../../behaviors/component');
import { getCartProductList, getCartProductTotal, cartChangeItem, clearCartData } from '../../../biztools/cart';
import { COMPONENT_OPTIONS } from '../../../constants/common';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
            observer: function (falg) {
                falg && this.refreshCartData();
            },
        },
        // 活动id
        activityId: {
            type: String,
            value: '',
        },
        // 最小起购金额
        minimumPurchaseAmount: {
            type: Number,
            value: '',
        },
        // 购物车层级
        zIndex: {
            type: Number,
            value: 1000,
        },
        // 页面是否有页脚
        isHaveFooter: {
            type: Boolean,
            value: true,
        },
        // 是否有下单按钮
        isHasEnsureBtn: {
            type: Boolean,
            value: true,
        },
    },
    data: {
        cartList: [], // 购物车数据
        cartTotal: {},
    },
    methods: {
        // 关闭
        onClose: function () {
            this.setData({
                show: false,
            });
            this.triggerEvent('close');
        },
        refreshCartData: function () {
            const { activityId } = this.data;
            const cartList = getCartProductList(activityId);
            const cartTotal = getCartProductTotal(activityId);
            this.setData({
                cartList,
                cartTotal,
            });
            this.triggerEvent('refresh');
        },
        // 清空操作
        clearAction: function () {
            const { activityId } = this.data;
            clearCartData(activityId);
            this.refreshCartData();
            this.onClose();
        },
        changeAction: function (event) {
            const { activityId } = this.data;
            cartChangeItem(event.detail, activityId);
            this.refreshCartData();
        },
        ensureOrder: function () {
            this.triggerEvent('ensure');
        },
    }
})
